package com.sykj.why.mongo.service.impl;

import com.sykj.why.exception.WhyException;
import com.sykj.why.mongo.dao.RelevantCertificateTDao;
import com.sykj.why.mongo.document.entity.RelevantCertificateT;
import com.sykj.why.mongo.enums.exception.ResultEnum;
import com.sykj.why.mongo.service.RelevantCertificateTService;
import com.sykj.why.util.DateTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Eleven
 * @date 2019/7/5
 * @desc 相关证明材料业务实现层
 */

@Service("RelevantCertificateTService")
public class RelevantCertificateTServiceImpl extends BasicServiceImpl<RelevantCertificateT> implements RelevantCertificateTService {

    @Autowired
    private RelevantCertificateTDao relevantCertificateTDao;

    /**
     * 传承人与证明材料绑定
     * @param relevantCertificateT
     */
    @Override
    public String saveRelevantCertificate(RelevantCertificateT relevantCertificateT) {
        //验证文件路径是否重复
        verifyUrl(relevantCertificateT);
        //排序
        long count = relevantCertificateTDao.count(RelevantCertificateT.class, false);
        relevantCertificateT.setSort((int) count);
        //设置时间
        relevantCertificateT.setCreateTime(DateTool.getSystemTime());
        relevantCertificateT.setUpdateTime(DateTool.getSystemTime());
        //保存
        relevantCertificateTDao.save(relevantCertificateT);
        return relevantCertificateT.getId();
    }

    /**
     * 验证文件路径
     * @param relevantCertificateT
     */
    public void verifyUrl(RelevantCertificateT relevantCertificateT) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").ne(relevantCertificateT.getId()).
                and("url").is(relevantCertificateT.getUrl()).and("removed").is(false));
        RelevantCertificateT relevantCertificate = relevantCertificateTDao.findOne(query,RelevantCertificateT.class);
        if (relevantCertificate != null) {
            throw new WhyException("请勿重复上传相同文件");
        }
    }

    /**
     * 解除传承人与相关证明材料绑定
     * @param id
     */
    @Override
    public void removeRelevantCertificate(String id) {
        RelevantCertificateT relevantCertificateT= findById(id);
        //逻辑删除相关证明材料
        relevantCertificateTDao.remove(relevantCertificateT);
    }

    /**
     * 修改传承人与相关证明材料绑定
     * @param relevantCertificateT
     */
    @Override
    public void updateRelevantCertificate(RelevantCertificateT relevantCertificateT) {
        //通过id查询相关证明材料
        RelevantCertificateT relevantCertificate = findById(relevantCertificateT.getId());
        //重新赋值
        //验证文件地址是否重复
        verifyUrl(relevantCertificateT);
        relevantCertificate.setName(relevantCertificateT.getName());
        relevantCertificate.setSize(relevantCertificateT.getSize());
        relevantCertificate.setExt(relevantCertificateT.getExt());
        relevantCertificate.setUrl(relevantCertificateT.getUrl());
        relevantCertificate.setUpdateTime(DateTool.getSystemTime());
        //保存或修改
        relevantCertificateTDao.saveOrupdate(relevantCertificate);
    }

    /**
     * 通过id查询相关证明材料
     * @param id
     * @return
     */
    @Override
    public RelevantCertificateT findById(String id) {
        //通过id查询未删除的相关证明材料
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id).and("removed").is(false));
        RelevantCertificateT relevantCertificateT = relevantCertificateTDao.findOne(query,RelevantCertificateT.class);
        if (relevantCertificateT!=null){
            return relevantCertificateT;
        }else {
            throw new WhyException(ResultEnum.OBJISNULL);
        }
    }

    /**
     * 通过用户查询相关证明材料列表
     * @param userId
     * @return
     */
    @Override
    public List<RelevantCertificateT> findByUser(String userId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("removed").is(false).and("userId").is(userId));
        List<RelevantCertificateT> list = relevantCertificateTDao.findAll(query,RelevantCertificateT.class);
        return list;
    }
}
